using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// Creates a Label control.
    /// </summary>
    public class Label : Control
    {
        #region Private Members
        private Enumerations.TextAlignmentTypes _textAlignment = Enumerations.TextAlignmentTypes.Left;
        private string _text = "Label";
        private SpriteFont _font;
        private bool _autoSize = false;
        #endregion

        #region Constructors
        /// <summary>
        /// Creates a Label Control.
        /// </summary>
        /// <param name="form">The Form that owns the conrol.</param>
        public Label(Form form) : base(form)
        {
            base.Size = new Vector2(100, 30);
            base.Location = new Vector2(10, 10);
            base.ForeColor = Color.White;
            base.BackColor = Drawing.TransparentColor();
            base.BorderType = Enumerations.BorderType.BorderSingle;
        }

        public Label(Control parent) : base(parent)
        {
            base.Size = new Vector2(100, 30);
            base.Location = new Vector2(10, 10);
            base.ForeColor = Color.White;
            base.BackColor = Drawing.TransparentColor();
            base.BorderType = Enumerations.BorderType.BorderSingle;
        }
        #endregion

        #region Overrides
        public override void LoadContent()
        {
            _font = base.Form.Content.Load<SpriteFont>("DefaultFont");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (base.Visible)
            {
                Vector2 textSize = _font.MeasureString(_text);

                if (_autoSize)
                {
                    _size = new Vector2(textSize.X + 4 + (_borderWidth * 2), textSize.Y + 4 + (_borderWidth * 2));
                    if (Right > 272)
                        _size = new Vector2(Right - 272, _size.Y);
                }

                List<string> lines = Utilities.GetLinesFromString(_font, _text, _location, _size);
                if (lines.Count > 1)
                {
                    _size = new Vector2(_size.X, (textSize.Y * lines.Count) + 4);
                    base.PreDraw();
                }

                Vector2 textLocation = new Vector2(base.Location.X + 2 + _borderWidth, _location.Y + _borderWidth);
                switch (_textAlignment)
                {
                    case Enumerations.TextAlignmentTypes.Right:
                        textLocation = new Vector2(base.Location.X + base.Size.X - textSize.X - 2 - _borderWidth, base.Location.Y);
                        break;
                    case Enumerations.TextAlignmentTypes.Center:
                        textLocation = new Vector2(base.Location.X + (base.Size.X / 2) - (textSize.X / 2), _location.Y);
                        break;
                }

                foreach (string line in lines)
                {
                    base.SpriteBatch.DrawString(_font, line, textLocation, _foreColor);
                    textLocation = new Vector2(textLocation.X, textLocation.Y + textSize.Y);
                }

                base.Draw(gameTime);
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the SpriteFont to use for drawing text.
        /// </summary>
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }

        /// <summary>
        /// Gets or sets whether the Label will autosize to its content.  Default is false.
        /// </summary>
        public bool AutoSize
        {
            get { return _autoSize; }
            set { _autoSize = value; }
        }

        /// <summary>
        /// Gets or sets the string to use as text.
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Gets or sets the Color for the border.
        /// </summary>
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }

        /// <summary>
        /// Gets or sets the width of the border.
        /// </summary>
        public int BorderWidth
        {
            get { return _borderWidth; }
            set { _borderWidth = value; }
        }

        /// <summary>
        /// Gets or sets the text alignment.  Default is Left.
        /// </summary>
        public Enumerations.TextAlignmentTypes TextAlignment
        {
            get { return _textAlignment; }
            set { _textAlignment = value; }
        }
        #endregion
    }
}